/*
 * 
 */
package fr.univtln.superplanning.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import fr.univtln.superplanning.api.ISuperplanningManager;
import fr.univtln.superplanning.api.actor.MainTeacher;
import fr.univtln.superplanning.api.study.Promotion;
import fr.univtln.superplanning.api.study.Section;
import fr.univtln.superplanning.api.study.Step;
import fr.univtln.superplanning.gui.MainWindow;

public class PromotionController implements IGenericController, ActionListener,
		ListSelectionListener {

	private MainWindow mainWindow; // view

	private ISuperplanningManager superplanningManager;

	private Promotion promotion;

	public PromotionController(ISuperplanningManager superplanningManager, MainWindow mainWindow,
			Promotion promotion) {
		this.mainWindow = mainWindow;
		this.superplanningManager = superplanningManager;
		this.promotion = promotion;
		this.initializeViewListeners();
	}

	public MainWindow getMainWindow() {
		return mainWindow;
	}

	public void setMainWindow(MainWindow mainWindow) {
		this.mainWindow = mainWindow;
	}

	public ISuperplanningManager getSuperplanningManager() {
		return superplanningManager;
	}

	public void setSuperplanningManager(ISuperplanningManager superplanningManager) {
		this.superplanningManager = superplanningManager;
	}

	public Promotion getPromotion() {
		return promotion;
	}

	public void setPromotion(Promotion promotion) {
		this.promotion = promotion;
	}

	public void initializeViewListeners() {
		// Retreive the components and add it's listener.

		// 1. For the selection / visualization
		mainWindow.getPromotionPanelListpromotionPanelList().addListSelectionListener(this);

		// 2. For the insertion / modification

		mainWindow.getPromotionPanelModifyPanelNewButton().addActionListener(this);
		mainWindow.getPromotionPanelModifyPanelAddButton().addActionListener(this);
		mainWindow.getPromotionPanelModifyPanelCancelButton().addActionListener(this);
		mainWindow.getPromotionPanelModifyPanelModifyButton().addActionListener(this);
		// TODO: where is the new button ?
	}

	/**
	 * Save a promotion according with the GUI fields.
	 */
	public void savePromotion() {

		String selectedStep = mainWindow.getPromotionPanelModifyPanelStepComboBox()
				.getSelectedItem().toString();
		String selectedMainTeacher = mainWindow.getPromotionPanelModifyPanelMainteacherComboBox()
				.getSelectedItem().toString();
		String selectedSection = mainWindow.getPromotionPanelModifyPanelSectionComboBox()
				.getSelectedItem().toString();

		/*
		 * System.out.println(selectedStep);
		 * System.out.println(selectedMainTeacher);
		 * System.out.println(selectedSection);
		 */

		MainTeacher mt = superplanningManager.getPersonManager().getMainTeacherByLastname(
				selectedMainTeacher);
		Step step = superplanningManager.getStudyManager().getStepByCode(selectedStep);
		Section section = superplanningManager.getStudyManager().getSectionByCode(selectedSection);

		if (mt != null) {
			if (step != null) {
				if (section != null) {
					promotion = new Promotion();
					promotion.setName(mainWindow.getPromotionPanelModifyPanelNameTextfield()
							.getText());
					promotion.setCode(mainWindow.getPromotionPanelModifyPanelCodeTextfield()
							.getText());
					promotion.setYear(Integer.parseInt(mainWindow
							.getPromotionPanelModifyPanelYearTextField().getText()));
					promotion.setStep(step);
					promotion.setMainTeacher(mt);
				} else
					System.err.println("Filiere non récupérée\n");
			} else
				System.err.println("Etape non récupérée\n");
		} else
			System.err.println("Teacher non récupéré\n");

		section.add(promotion);

		superplanningManager.getStudyManager().insertPromotion(promotion);
		superplanningManager.getStudyManager().insertSection(section);

	}

	@Override
	public void updateUi() {
		promotion = null;

		/* Remove all items from every list of the panel */
		mainWindow.getPromotionPanelListpromotionPanelList().setModel(new DefaultListModel());
		mainWindow.getPromotionPanelModifyPanelSectionComboBox().removeAllItems();
		mainWindow.getPromotionPanelModifyPanelStepComboBox().removeAllItems();
		mainWindow.getPromotionPanelModifyPanelMainteacherComboBox().removeAllItems();

		/* Reload the promotions */
		List<String> promotions = superplanningManager.getStudyManager().getAllPromotionCodes();
		mainWindow.getPromotionPanelListpromotionPanelList().setListData(promotions.toArray());

		/* Reload the section combobox */
		List<String> sections = superplanningManager.getStudyManager().getAllSectionCodes();
		for (String s : sections) {
			mainWindow.getPromotionPanelModifyPanelSectionComboBox().addItem(s);
		}

		/* Reload the step combobox */
		List<String> steps = superplanningManager.getStudyManager().getAllStepCodes();
		for (String s : steps) {
			mainWindow.getPromotionPanelModifyPanelStepComboBox().addItem(s);
		}

		/* Reload the main teacher combobox */
		List<String> teachers = superplanningManager.getPersonManager().getAllMainTeachers();
		for (String t : teachers) {
			mainWindow.getPromotionPanelModifyPanelMainteacherComboBox().addItem(t);
		}
	}

	public void clearFields() {
		mainWindow.getPromotionPanelModifyPanelCodeTextfield().setText("");
		mainWindow.getPromotionPanelModifyPanelNameTextfield().setText("");
		mainWindow.getPromotionPanelModifyPanelYearTextField().setText("");

	}

	public void updateCurrentPromotionView() {

		String selectedPromotion = (String) mainWindow.getPromotionPanelListpromotionPanelList()
				.getSelectedValue();

		// 1. Get the promotion by it's code
		promotion = superplanningManager.getStudyManager().getPromotionByCode(selectedPromotion);

		if (promotion != null) {
			// Update the view

			mainWindow.getPromotionPanelModifyPanelCodeTextfield().setText(promotion.getCode());
			mainWindow.getPromotionPanelModifyPanelNameTextfield().setText(promotion.getName());
			mainWindow.getPromotionPanelModifyPanelYearTextField().setText(
					String.valueOf(promotion.getYear()));
			mainWindow.getPromotionPanelModifyPanelStepComboBox().setSelectedItem(
					promotion.getStep());

			mainWindow.getPromotionPanelModifyPanelMainteacherComboBox().setSelectedItem(
					promotion.getMainTeacher());

			// planningViewer.getPromotionPanelModifyPanelSectionComboBox().setSelectedItem(anObject);
		} else
			System.out.println("Promo inconnue");

	}

	@Override
	public void actionPerformed(ActionEvent evt) {
		/* Creation d'une promotion */
		if (evt.getSource() == mainWindow.getPromotionPanelModifyPanelNewButton()) {
			try {
				this.clearFields();
				// this.updateUi();

			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		else if (evt.getSource() == mainWindow.getPromotionPanelModifyPanelAddButton()) {
			try {
				this.savePromotion();
				this.updateUi();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if (e.getSource() == mainWindow.getPromotionPanelListpromotionPanelList()) {
			updateCurrentPromotionView();
		}
	}

}
